<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>商品添加</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript" charset="utf-8" src="${ctxStatic}/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="${ctxStatic}/ueditor/ueditor.all.js"> </script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="${ctxStatic}/ueditor/lang/zh-cn/zh-cn.js"></script>
	<script type="text/javascript">
		$(document).ready(function() {
			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					changeAttr();
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		
		function changeAttr(){
			
			 $('#eventSkuList').find('tr').each(function () {
				 	//遍历td获取值
		            $(this).find('td').each(function () {    
		            	$(this).find("select").each(function(i,rows){
		            		$(this).attr("disabled",false);
		    			});
		            })
		        })
		}
		
		function addRow(list, idx, tpl, row){
			
			$(list).append(Mustache.render(tpl, {
				idx: idx, delBtn: true, row: row
			}));
			$(list+idx).find("select").each(function(){
				$(this).val($(this).attr("data-value"));
				//$(this).attr("disabled","true");
			});
			
			$(list+idx).find("input[type='checkbox']").each(function(){
				var ss = $(this).attr("data-value");
				for (var i=0; i<ss.length; i++){
					if($(this).val() == ss[i]){
						$(this).attr("checked","checked");
					}
				}
			});
			$("#msg").remove();
		}
		function delRow(obj, prefix){
			var id = $(prefix+"_id");
			var delFlag = $(prefix+"_delFlag");
			if (id.val() == ""){
				$(obj).parent().parent().remove();
			}else if(delFlag.val() == "0"){
				
				$.post("${ctx}/mer/spu/deleteSku",{idFlag:id.val()}, function(result){
				    if(result.code =="0"){
				    	$(obj).parent().parent().remove();
				    }else{
				    	alertx(result.msg);
				    }
				  });
				//delFlag.val("1");
				//$(obj).html("&divide;").attr("title", "撤销删除");
				//$(obj).parent().parent().addClass("error");
			}else if(delFlag.val() == "1"){
				delFlag.val("0");
				$(obj).html("&times;").attr("title", "删除");
				$(obj).parent().parent().removeClass("error");
			}
		}
		
		//批量新增
		function batchAddRow(){
			
			 var  arr = [];
			 
			 var html = '';    
			 
			//获取tbody id="eventSkuList" 所有值
			 $('#eventSkuList').find('tr').each(function () {
				 	//遍历td获取值
		            $(this).find('td').each(function () {    
		            	$(this).find("select").each(function(){
		            		var a = $(this).val();
		            		html +=a
		    			});
		            })
		            arr.push(html)
		            html='';
		        })
			 
		       //console.log(arr); 
		        
			var data = []
			//遍历选中颜色
			$("#color").find("input[type='checkbox']:checked").each(function(){
				
				var colorId = $(this).val();
				
				//遍历选中尺码
				$("#size").find("input[type='checkbox']:checked").each(function(){
					
					var sizeId = $(this).val();
					var cs =colorId+sizeId;
					//console.log(sizeId);
					//console.log(cs);
					var str = arr.join();
					//console.log(str); 
					if(str.indexOf(cs)==-1){
						//把每个组合放到data数组
						data.push({colorId:colorId,sizeId:sizeId});
						//console.log(data);
					}
				});
			});
			for (var i=0; i<data.length; i++){
				addRow('#eventSkuList', eventSkuRowIdx, eventSkuTpl,data[i]);
				eventSkuRowIdx = eventSkuRowIdx + 1;
			}
			
		}
		
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li class="active"><a href="${ctx}/mer/spu/form?id=${spu.id}">商品<shiro:hasPermission name="mer:spu:edit">${not empty spu.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="mer:spu:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="spu" action="${ctx}/mer/spu/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<form:hidden path="classifyId"/>
		<sys:message content=""/>		
		<div class="control-group">
			<label class="control-label">商品名称：</label>
			<div class="controls">
				<form:input path="name" htmlEscape="false" maxlength="64" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">商品图片：<span class="help-inline"><font color="red">(正方形500*500px)</font></span></label>
			<div class="controls">
				<form:hidden id="nameImage" path="photo" htmlEscape="false" maxlength="255" class="input-xlarge"/>
				<sys:ckfinder input="nameImage" type="images" uploadPath="/mer/spu" selectMultiple="false" maxWidth="100" maxHeight="100"/>
				<span>标签：</span>
				<form:select path="tag5" class="input-xlarge" multiple="multiple" size="1">
					<form:options items="${fns:getTag()}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
				<span class="help-inline"><font color="red">${tag5 }</font> </span>
			</div>
			<div class="controls">
				<form:hidden id="nameImage1" path="photo1" htmlEscape="false" maxlength="255" class="input-xlarge"/>
				<sys:ckfinder input="nameImage1" type="images" uploadPath="/mer/spu" selectMultiple="false" maxWidth="100" maxHeight="100"/>
				<%-- <span>标签：</span>
				<form:select path="tag6" class="input-xlarge" multiple="multiple">
					<form:options items="${fns:getTag()}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
				<span class="help-inline"><font color="red">${tag6 }</font> </span> --%>
			</div>
			<div class="controls">
				<form:hidden id="nameImage2" path="photo2" htmlEscape="false" maxlength="255" class="input-xlarge"/>
				<sys:ckfinder input="nameImage2" type="images" uploadPath="/mer/spu" selectMultiple="false" maxWidth="100" maxHeight="100"/>
				<%-- <span>标签：</span>
				<form:select path="tag7" class="input-xlarge" multiple="multiple">
					<form:options items="${fns:getTag()}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
				<span class="help-inline"><font color="red">${tag7 }</font> </span> --%>
			</div>
			<div class="controls">
				<form:hidden id="nameImage3" path="photo3" htmlEscape="false" maxlength="255" class="input-xlarge"/>
				<sys:ckfinder input="nameImage3" type="images" uploadPath="/mer/spu" selectMultiple="false" maxWidth="100" maxHeight="100"/>
				<%-- <span>标签：</span>
				<form:select path="tag8" class="input-xlarge" multiple="multiple">
					<form:options items="${fns:getTag()}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
				<span class="help-inline"><font color="red">${tag8 }</font> </span> --%>
			</div>
			<div class="controls">
				<form:hidden id="nameImage4" path="photo4" htmlEscape="false" maxlength="255" class="input-xlarge"/>
				<sys:ckfinder input="nameImage4" type="images" uploadPath="/mer/spu" selectMultiple="false" maxWidth="100" maxHeight="100"/>
				<%-- <span>标签：</span>
				<form:select path="tag9" class="input-xlarge" multiple="multiple">
					<form:options items="${fns:getTag()}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
				<span class="help-inline"><font color="red">${tag9 }</font> </span> --%>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">商品编号：</label>
			<div class="controls">
				<form:input path="code" htmlEscape="false" maxlength="64" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<c:if test="${not empty spu.brandList }">
			<div class="control-group">
				<label class="control-label">品牌：</label>
				<div class="controls">
					<form:select path="brandId" class="input-xlarge ">
						<form:option value="" label=" "/>
						<form:options items="${spu.brandList }" itemValue="id" itemLabel="name" />
					</form:select>
				</div>
			</div>
		</c:if>
		<c:if test="${not empty spu.supplierList }">
			<div class="control-group">
				<label class="control-label">供应商：</label>
				<div class="controls">
					<form:select path="supplierId" class="input-xlarge ">
						<form:option value="" label=" "/>
						<form:options items="${spu.supplierList }" itemValue="id" itemLabel="name" />
					</form:select>
				</div>
			</div>
		</c:if>
		<%--<div class="control-group">
			<label class="control-label">推荐图片:<span class="help-inline"><font color="red">(宽、高不得低于500*</font></span><span class="help-inline"><font color="red">216px,按等比例上传)</font></span></label>
			<div class="controls">
				<form:hidden id="nameImage5" path="recommendPhoto" htmlEscape="false" maxlength="255" class="input-xlarge"/>
				<sys:ckfinder input="nameImage5" type="images" uploadPath="/mer/spu" selectMultiple="false" maxWidth="100" maxHeight="100"/>
			</div>
		</div> --%>
		<c:if test="${not empty spu.attrList }">
			<c:forEach items="${spu.attrList }" var="vs" varStatus="v">
				<div class="control-group">
					<label class="control-label">${vs.name }：</label>
					<div class="controls">
						<input type="hidden" name="attrList[${v.index}].attributeValue.attributeId" value="${vs.id}"/>
						<input type="hidden" name="attrList[${v.index}].attributeValue.spuId" value="${vs.attributeValue.spuId}"/>
						<input type="hidden" name="attrList[${v.index}].attributeValue.id" value="${vs.attributeValue.id}"/>
						<input name="attrList[${v.index}].attributeValue.value"  value="${vs.attributeValue.value }" htmlEscape="false" maxlength="255" class="input-xlarge "/>
					</div>
				</div>
			</c:forEach>
		</c:if>
		
		<div class="control-group">
			<label class="control-label">排序：</label>
			<div class="controls">
				<form:input path="sort" htmlEscape="false" maxlength="11" class="input-xlarge digits"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">是否上架：</label>
			<div class="controls">
				<form:select path="status" class="input-xlarge ">
					<form:option value="0">上架</form:option>
					<form:option value="1">下架</form:option>
				</form:select>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">首页推荐：</label>
			<div class="controls">
				<form:select path="homeRecommend" class="input-xlarge ">
					<form:option value="1">否</form:option>
					<form:option value="0">是</form:option>
				</form:select>
			</div>
		</div>
		<div class="control-group">
				<label class="control-label">颜色：</label>
				<div class="controls" id="color">
					<form:checkboxes items="${spu.colorList}"  path="" itemLabel="name" itemValue="id"/>
				</div></br>
				<label class="control-label">尺寸：</label>
				<div class="controls" id="size">
					<form:checkboxes items="${spu.sizeList}"  path="" itemLabel="name" itemValue="id"/>
				</div></br>
				<div class="controls">
					<a id="batchAddRow" href="javascript:" onclick="batchAddRow('#eventSkuList');" class="btn">新增</a>
					<!-- <input id="batchAddRow" class="btn" type="button" value="批量添加" onclick="batchAddRow();"/> -->
					<!-- <button onclick="batchAddRow()">批量添加</button> -->
				</div></br>
				<label class="control-label">属性添加：</label>
				<div class="controls">
					<table id="contentTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th class="hide"></th>
								<th>颜色</th>
								<th>尺码</th>
								<th>价格</th>
								<th>数量</th>
								<shiro:hasPermission name="mer:spu:edit"><th width="10">删除</th></shiro:hasPermission>
							</tr>
						</thead>
						<tbody id="eventSkuList">
						</tbody>
						<%-- <shiro:hasPermission name="mer:spu:edit"><tfoot>
							<tr><td colspan="10"><a href="javascript:" onclick="addRow('#eventSkuList', eventSkuRowIdx, eventSkuTpl);eventSkuRowIdx = eventSkuRowIdx + 1;" class="btn">新增</a></td></tr>
						</tfoot></shiro:hasPermission> --%>
					</table>
					<script type="text/template" id="eventSkuTpl">//<!--
						<tr id="eventSkuList{{idx}}">
							<td class="hide">
								<input id="eventSkuList{{idx}}_delFlag" name="eventSkuList[{{idx}}].delFlag" type="hidden" value="0"/>
								<input id="eventSkuList{{idx}}_spuId" name="eventSkuList[{{idx}}].spuId" type="hidden" value="{{row.spuId}}"/>
								<input id="eventSkuList{{idx}}_id" name="eventSkuList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								
							</td>
							<td>
								<select name="eventSkuList[{{idx}}].colorId" data-value="{{row.colorId}}" id="colorId" disabled="true">
									<option value=""></option>
									<c:forEach items="${spu.colorList}" var="v">
										<option value="${v.id}">${v.name}</option>
									</c:forEach>
								</select>
							</td>
							<td>
								<select name="eventSkuList[{{idx}}].sizeId" data-value="{{row.sizeId}}" id="sizeId" disabled="true">
									<option value=""></option>
									<c:forEach items="${spu.sizeList}" var="v">
										<option value="${v.id}">${v.name}</option>
									</c:forEach>
								</select>
							</td>
							<td>
								<input id="eventSkuList{{idx}}_price" name="eventSkuList[{{idx}}].price" type="text" value="{{row.price}}" maxlength="45" class="input-small required number"/>
							</td>
							<td>
								<input id="eventSkuList{{idx}}_num" name="eventSkuList[{{idx}}].num"  type="text" value="{{row.num}}" maxlength="8" class="input-small digits"/>
							</td>
							<shiro:hasPermission name="mer:spu:edit"><td class="text-center" width="10">
								{{#delBtn}}<span class="close" onclick="delRow(this, '#eventSkuList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
							</td></shiro:hasPermission>
						</tr>//-->
					</script>
					<script type="text/javascript">
						var eventSkuRowIdx = 0, eventSkuTpl = $("#eventSkuTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
						$(document).ready(function() {
							var data = ${fns:toJson(spu.eventSkuList)};
							for (var i=0; i<data.length; i++){
								addRow('#eventSkuList', eventSkuRowIdx, eventSkuTpl, data[i]);
								eventSkuRowIdx = eventSkuRowIdx + 1;
							}
						});
					</script>
					<div id="msg"><label class="control-label"><font color="red">${msg}</font></label></div>
				</div>
			</div>
		<div class="control-group">
			<label class="control-label">描述：</br><span class="help-inline"><font color="red">(22个字)</font></span></label>
			<div class="controls">
				<form:textarea path="simpleDescription" htmlEscape="false" rows="3" maxlength="22" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font></span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">备注：</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="255" class="input-xxlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">商品图文详情：<span class="help-inline"><font color="red">图片宽500像素</font></span>
			<span class="help-inline"><font color="red">高度800-1200像素</font></span>
			<span class="help-inline"><font color="red">单个图片不超过200k</font></span></label>
			<div class="controls">
				<%-- <form:input path="productDescription" htmlEscape="false" class="input-xlarge "/> --%>
				<form:textarea id="description" htmlEscape="false" path="description" rows="4" maxlength="200" class="input-xxlarge" />
				<%-- <sys:ckeditor replace="description" uploadPath="/mer/spu" /> --%>
				<script type="text/javascript">
        			var ue = UE.getEditor('description', {
        				initialFrameWidth: 800,//设置为null即可
        				initialFrameHeight: 425});
   				 </script>
			</div>
		</div>
		<div class="form-actions">
			<shiro:hasPermission name="mer:spu:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
</body>
</html>